package com.zhihesj.anan.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhihesj.anan.message.Message;
import com.zhihesj.anan.message.MessageCodeEnum;
import com.zhihesj.anan.system.entity.Dept;
import com.zhihesj.anan.system.service.DeptService;
import com.zhihesj.anan.system.vo.DeptVo;
import com.zhihesj.anan.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/addDept")
    public Message addDept(@RequestBody @Valid Dept dept){
        if (!dept.toString().isEmpty()) {
            Long parentId = dept.getParentId()==null ? 100L : dept.getParentId();
            Dept dept1 = deptService.getById(parentId);
            dept.setAncestors(dept1.getAncestors() +'.'+ dept1.getDeptId());
            deptService.save(dept);
            return Message.success(MessageCodeEnum.ADD_SUCCESS);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteDept")
    public Message deleteDept(Long deptId){
        if (!deptId.toString().isEmpty()) {
            deptService.removeById(deptId);
            return Message.success(MessageCodeEnum.DELETE_SUCCESS);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByDeptId")
    public Message deleteByDeptId(@RequestBody DeptVo deptVo){
        if (deptVo.getChildren()!=null && deptVo.getChildren().size()!=0){
            return Message.success(MessageCodeEnum.HAVE_CHILDRENDEPT_NOT_DELETE);
        }
        if (deptService.removeById(deptVo.getDeptId())){
            return Message.success(MessageCodeEnum.SUCCESS);
        }
        return Message.success(MessageCodeEnum.DELETE_ERROR);
    }

    @DeleteMapping("/deleteDepts")
    public Message deleteDepts(List<Long> deptIds){
        if (!deptIds.isEmpty()) {
            for (Long deptid:deptIds) {
                deleteDept(deptid);
            }
            return Message.success(MessageCodeEnum.SUCCESS);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @PostMapping("/updateDeptById")
    public Message updateDeptById(@RequestBody @Valid Dept dept){
        if (!dept.toString().isEmpty()) {
            deptService.updateById(dept);
            return Message.success(MessageCodeEnum.SUCCESS);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @RequestMapping("/updateDepts")
    public Message updateDepts(@Valid List<Dept> depts){
        if (!depts.isEmpty()) {
            for (Dept dept:depts) {
                updateDeptById(dept);
            }
            return Message.success(MessageCodeEnum.SUCCESS);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @RequestMapping("/getDept")
    public Message getDept(Long deptId){
        if (!deptId.toString().isEmpty()) {
            Dept dept=deptService.getById(deptId);
            return Message.success(dept);
        }
        return Message.error(MessageCodeEnum.OPERATION_ERROR);
    }

    @GetMapping("/getDeptList")
    public Message getDeptList(DeptVo deptVo){
        System.out.println(deptVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(deptVo, false, true);
        List<Dept> depts=deptService.list();
        BaseUtil baseUtil =new BaseUtil();
        if (stringObjectMap.isEmpty() || deptVo.getParentId()==null){
            List<DeptVo> list = baseUtil.ArrangementDeptTree(depts, 0L);
            return Message.success(list);
        }
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deptVo.getParentId());
        List<Dept> iPageDept=deptService.selectPage(deptVo);
        List<DeptVo> deptVoList= baseUtil.ArrangementDeptTree(iPageDept,depts);
        return Message.success(deptVoList);
    }

}
